<?php

/**
 * ecshop ERP v1.0
 * ----------------------------------------------------------------------------
 * http://www.phpally.com
 * Jacklee的博客 致力于php技术
 * ----------------------------------------------------------------------------
 * 作者: Jacklee
 * 邮箱: jack349392900@gmail.com
 * 创建时间: 2013-11-18
 * 最后修改时间: 2013-11-18
 */

require_once(dirname(__FILE__) . '/lib_erp_common.php');

function get_supplier_group($id='',$start=-1,$num=-1)
{
	if(empty($id))
	{
		$sql="select * from ".$GLOBALS['ecs']->table('erp_supplier_group')." where 1";
		
		if($start!=-1 && $num!=-1)
		{
			if($start<=0)
			{
				$start=0;
				}
			$sql.=" limit ".$start.",".$num;
			
		}	

		$supplier_groups=$GLOBALS['db']->getAll($sql);
		
		if(!empty($supplier_groups))
		{
			foreach($supplier_groups as $key=>$item)
			{
				$admin_info=get_admin($item['admin_id']);
				$supplier_groups[$key]['admin_name']=$admin_info['user_name'];
			}
		}
		
		return $supplier_groups;
	}
	else{
		$sql="select * from ".$GLOBALS['ecs']->table('erp_supplier_group')." where group_id='".$id."'";
		$supplier_group=$GLOBALS['db']->getRow($sql);
		$supplier_group['admin_name']=get_admin($supplier_group['admin_id']);
		return $supplier_group;		
	}
}

function delete_supplier_group($id)
{
	$sql="select count(*) as num from ".$GLOBALS['ecs']->table('erp_supplier')." where supplier_group_id='".$id."'";
	$num=$GLOBALS['db']->getOne($sql);
	if(!empty($num))
	{
		return false;
	}
	else{
		$sql="delete from ".$GLOBALS['ecs']->table('erp_supplier_group')." where group_id='".$id."' limit 1";
		$GLOBALS['db']->query($sql);
		return true;
	}
}

function get_admin_supplier($admin_id)
{
	$sql="select * from ".$GLOBALS['ecs']->table('erp_supplier')." where is_valid='1'";
	$supplier_info1=$GLOBALS['db']->getAll($sql);
	
	$sql="select * from ".$GLOBALS['ecs']->table('erp_supplier')." where is_valid='1'";
	$supplier_info2=$GLOBALS['db']->getAll($sql);

	$supplier_id_array=array();
	$supplier_info=array();
	
	if(!empty($supplier_info1))
	{
		foreach($supplier_info1 as $key => $item)
		{
			$supplier_id=$item['supplier_id'];	
			if(!in_array($supplier_id,$supplier_id_array))
			{
				array_push($supplier_info,$supplier_info1[$key]);
				array_push($supplier_id_array,$supplier_id);
			}			
		}		
	}

	if(!empty($supplier_info2))
	{
		foreach($supplier_info2 as $key => $item)
		{
			$supplier_id=$item['supplier_id'];	
			if(!in_array($supplier_id,$supplier_id_array))
			{
				array_push($supplier_info,$supplier_info2[$key]);
				array_push($supplier_id_array,$supplier_id);
			}			
		}		
	}	
	
	if(!empty($supplier_info))
	{
		foreach($supplier_info as $key=>$item)
		{
				$sql="select group_name from ".$GLOBALS['ecs']->table('erp_supplier_group')." where group_id='".$item['supplier_group_id']."'";
				$supplier_info[$key]['supplier_group_name']=$GLOBALS['db']->getOne($sql);
				
				$admin_info=get_admin($item['admin_id']);
				$supplier_info[$key]['admin_name']=$admin_info['user_name'];				
			}
	}
	return 	$supplier_info;
	
}

function get_supplier_info($supplier_group_id='',$supplier_id='',$supplier_code='',$start=-1,$num=-1)
{
	if(empty($supplier_id) && empty($supplier_code))
	{
		$sql="select * from ".$GLOBALS['ecs']->table('erp_supplier')." where 1 ";

		if(!empty($supplier_group_id))
		{
			$sql.=" and supplier_group_id='".$supplier_group_id."'";
		}
		
		if($start!=-1 && $num!=-1)
		{
			if($start<=0)
			{
				$start=0;
				}
			$sql.=" limit ".$start.",".$num;
		}
	}
	elseif(!empty($supplier_id) && empty($supplier_code))
	{
		$sql="select * from ".$GLOBALS['ecs']->table('erp_supplier')." where supplier_id='".$supplier_id."'";	
	}
	elseif(empty($supplier_id) && !empty($supplier_code))
	{
		$sql="select * from ".$GLOBALS['ecs']->table('erp_supplier')." where code='".$supplier_code."'";
	}
	
	$supplier_info=$GLOBALS['db']->getAll($sql);
	
	if(!empty($supplier_info))
	{
		foreach($supplier_info as $key=>$item)
		{
				$sql="select group_name from ".$GLOBALS['ecs']->table('erp_supplier_group')." where group_id='".$item['supplier_group_id']."'";
				$supplier_info[$key]['supplier_group_name']=$GLOBALS['db']->getOne($sql);
				
				$admin_info=get_admin($item['admin_id']);
				$supplier_info[$key]['admin_name']=$admin_info['user_name'];				
			}
	}
	return 	$supplier_info;
}

// function suppliers_list_name()
// {
	// $suppliers=get_supplier_info();
	// $result=array();
	// if(!empty($suppliers))
	// {
		// foreach($suppliers as $key => $item)
		// {
			// $result[$item['supplier_id']]=$item['name'];
		// }
	// }
	
	// return $result;
// }

function gen_supplier_code()
{
	$sql="select code from ".$GLOBALS['ecs']->table('erp_supplier')." order by code desc limit 1";
	$result=$GLOBALS['db']->getOne($sql);
	$result=intval($result)+1;
	return format_code($result);
}


/**
 * 确定供应商存在，存在返回供应商信息.
 * @param  
 * $supplier_id 成品供应商序号
 * $code 成品供应商编号
 */

function is_supplier_exist($supplier_id='',$code='')
{
	if(empty($supplier_id) && empty($code))
	{
		return false;
	}
	elseif(!empty($supplier_id) && empty($code))
	{
		$sql="select * from ".$GLOBALS['ecs']->table('erp_supplier')." where supplier_id='".$supplier_id."'";
		
	}
	elseif(empty($supplier_id) && !empty($code))
	{
		$sql="select * from ".$GLOBALS['ecs']->table('erp_supplier')." where code ='".$code."'";
	}
	
	$result=$GLOBALS['db']->getRow($sql);
	if($result!==false)
	{
		return $result;
	}
	else{
		return false;
	}
}

?>